/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccesoBusquedas;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.table.DefaultTableModel;
import sifact.conexion.cConexion;
/**
 *
 * @author Cyber Huevadictos
 */
public class AB_Proveedor {
    private Object [] fila;
    private DefaultTableModel modelo;
    private Connection cn;
    private Statement st;
    //Constructor
    public AB_Proveedor(){

        try {
            this.cn = cConexion.estado();
            this.st = this.cn.createStatement();
        } catch (Exception ex) {
//            Logger.getLogger(cFiltroTabla.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //
    public DefaultTableModel TProveedor(String FILTRO,int num) throws Exception{
         this.modelo = new DefaultTableModel();
         //CREO LAS COLUMNAS
         this.modelo.addColumn("EMPRESA");         
         this.modelo.addColumn("REPRESENTANTE");
         this.modelo.addColumn("CARGO");
         this.modelo.addColumn("CIUDAD");
         //
         this.cn = cConexion.estado();
         this.st = this.cn.createStatement();
         switch(num){
             case 1:try{// aqui es la condicion 1
                        ResultSet rs  = st.executeQuery("select \"txtNomEmpresa\",\"txtNomContacto\",\"txtCargoContacto\",\"txtCiudad\" from public.\"Proveedores\" where \"txtNomEmpresa\" LIKE '%"+
                        FILTRO + "%'"+" order by \"txtNomEmpresa\"");
                        while(rs.next()){
                            this.fila = new Object[4];
                        for (int i = 0;i < 4;i++) {
                                this.fila[i] = rs.getObject(i+1);
                            }

                        modelo.addRow(fila);
                        }
                        rs.close();
                    }catch(Exception ex){ }
                    break;
             case 2: try{// aqui es la condicion 2
                        ResultSet rs  = st.executeQuery("select \"txtNomEmpresa\",\"txtNomContacto\",\"txtCargoContacto\",\"txtCiudad\" from public.\"Proveedores\" where \"txtNomContacto\" LIKE '%"+
                        FILTRO + "%'"+" order by \"txtNomContacto\"");
                        while(rs.next()){
                            this.fila = new Object[4];
                        for (int i = 0;i < 4;i++) {
                                this.fila[i] = rs.getObject(i+1);
                            }

                        modelo.addRow(fila);
                        }
                        rs.close();
                    }catch(Exception ex){  }
                    break;
             case 3: try{// aqui es la condicion 3
                        ResultSet rs  = st.executeQuery("select \"txtNomEmpresa\",\"txtNomContacto\",\"txtCargoContacto\",\"txtCiudad\" from public.\"Proveedores\" where \"txtCiudad\" LIKE '%"+
                        FILTRO + "%'"+" order by \"txtCiudad\"");
                        while(rs.next()){
                            this.fila = new Object[4];
                        for (int i = 0;i < 4;i++) {
                                this.fila[i] = rs.getObject(i+1);
                            }

                        modelo.addRow(fila);
                        }
                        rs.close();
                    }catch(Exception ex){ }
                   break;
             default: try{// la primera vez lleno la tabla sin condiciones
                        ResultSet rs  = st.executeQuery("select \"txtNomEmpresa\",\"txtNomContacto\",\"txtCargoContacto\",\"txtCiudad\" from public.\"Proveedores\" order by \"numIDProveedor\"");
                        while(rs.next()){
                            this.fila = new Object[4];
                        for (int i = 0;i < 4;i++) {
                                this.fila[i] = rs.getObject(i+1);
                            }

                        modelo.addRow(fila);
                        }
                        rs.close();
//            jTable1.setModel(modelo);
                    }catch(Exception ex){
//            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
//            ex.printStackTrace();
                    }
                    break;
         }
         cn.close();
         st.close();
         return modelo;

    }
}
